package initialization;

import fundamental.*;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

import static fundamental.Params.*;

public class ProblemLoad {
    public static void readBenchMark(String infoFile, String distFile, int n) {
        try {
            BufferedReader infoBr = new BufferedReader(new FileReader(infoFile));
            BufferedReader distBr = new BufferedReader(new FileReader(distFile));
            depotNum = 2;
            candNum = n;
            custNum = candNum;
            for (int i = 0; i < depotNum; i++) {
                Depot depot = new Depot(i);
                depotList.add(depot);
                depotSet.add(depot);
            }
            for (int i = 0; i < candNum; i++) {
                String[] info = infoBr.readLine().split(" ");
                Cand cand = new Cand(Integer.parseInt(info[0]), cFix);
                Customer cust = new Customer(Integer.parseInt(info[0]), Integer.parseInt(info[1]),cap2);
                cand.sameCust = cust;
                cust.sameCand = cand;
                candList.add(cand);
                candSet.add(cand);
                custList.add(cust);
                custSet.add(cust);
            }
            chaoCandList = new ArrayList<>(candList);
            dist = new double[depotNum+candNum][depotNum+candNum];
            for (int i = 0; i < depotNum+candNum; i++) {
                String[] temp = distBr.readLine().split(" ");
                for (int j = 0; j < depotNum+candNum; j++) {
                    dist[i][j] = Double.parseDouble(temp[j]);
                }
            }
            getSortByDist(dist);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void getSortByDist(double[][] dist) {
        for (Cand cand: candList
             ) {
            ArrayList<Cand> sortTemp = new ArrayList<>();
            for (Cand c: candList
                 ) {
                if (c != cand) sortTemp.add(c);
            }
            sortTemp.sort(Comparator.comparingDouble(o -> dist[o.num][cand.num]));
            candSortForDist.put(cand, sortTemp);
        }
    }

    public static void saveTxt() {
        String openedFile = "开放的网点.txt";
        String secVFile = "第二级车辆路径.txt";
        String firVFile = "第一级车辆路径.txt";
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(openedFile));
            StringBuilder sb = new StringBuilder();
            for (Cand cand: lsObj.bestOpened
                 ) {
                sb.append(cand.num);
                sb.append(" ");
            }
            bw.write(sb.toString());
            bw.flush();
            bw.close();
            bw = new BufferedWriter(new FileWriter(secVFile));
            for (SecondVehicle v: lsObj.secVehicleList
                 ) {
                if (v.route.size() == 2 || (v.route.size() == 3 && v.route.get(1) == v.start_end.sameCust)) continue;
                sb = new StringBuilder();
                for (Point p: v.route
                     ) {
                    sb.append(p.num);
                    sb.append(" ");
                }
                bw.write(sb.toString());
                bw.newLine();
            }
            bw.flush();
            bw.close();
            bw = new BufferedWriter(new FileWriter(firVFile));
            for (FirstVehicle v: lsObj.firVehicleList
                 ) {
                if (v.route.size() == 2) continue;
                sb = new StringBuilder();
                for (Point p: v.route
                ) {
                    sb.append(p.num);
                    sb.append(" ");
                }
                bw.write(sb.toString());
                bw.newLine();
            }
            bw.flush();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
